package training.project.app.repository.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import training.project.app.domain.Flight;
import training.project.app.domain.FlightClient;
import training.project.app.repository.FlightDao;
import training.project.app.repository.base.AbstractHibernateDao;

@Repository
@Transactional
public class FlightRepository extends AbstractHibernateDao<Flight, Integer>
		implements FlightDao
	{
		@Override
		public Flight findById(Integer flightId)
			{
				Criteria cr = getSession().createCriteria(Flight.class,
						"flight").add(Restrictions.eq("id", flightId));
				return (Flight) cr.uniqueResult();
			}

		@Override
		public void increasePrice(Flight flight)
			{
				flight.setPrice(flight.getPrice() * 1.05F);
				System.out.println(flight.getPrice());
			}

		@SuppressWarnings("unchecked")
		public List<FlightClient> getFlightClients(Integer flightId)
			{
				Criteria crit = getSession().createCriteria(FlightClient.class,
						"FlightClient");
				crit.add(Restrictions.eq("flight.clientId", flightId));
				return (List<FlightClient>) crit.list();
			}

		@SuppressWarnings("unchecked")
		@Override
		public List<Flight> findFlighs(String startPlace, String endPlace)
			{
				Criteria cr = getSession().createCriteria(Flight.class,
						"flight");
				if (startPlace != null && startPlace != "")
					{
						cr.add(Restrictions.eq("startPlace", startPlace));
					}
				if (endPlace != null  && !endPlace.isEmpty())
					{
						cr.add(Restrictions.eq("endPlace", endPlace));
					}
				cr.addOrder(Order.asc("departureTime"));
				return (List<Flight>) cr.list();
			}

	
	}
